<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">

      <div class="table-page-search-wrapper" v-if="hasPerm('LesTakematerials:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="单据号">
                <a-input v-model="queryParam.OrderNo" allow-clear placeholder="请输入..."/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select :allowClear="true" style="width: 100%" v-model="queryParam.status" placeholder="请选择状态">
                  <a-select-option v-for="(item,index) in statusData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="8" :sm="24">
              <a-form-item label="计划号">
                <a-input v-model="queryParam.planNo" allow-clear placeholder="请输入计划号"/>
              </a-form-item>
            </a-col> -->
            <!-- <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="叫料站点">
                  <a-input v-model="queryParam.takeMaterialsSite" allow-clear placeholder="请输入叫料站点"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="班组">
                  <a-input v-model="queryParam.teamName" allow-clear placeholder="请输入班组"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="物料名称">
                  <a-input v-model="queryParam.materialName" allow-clear placeholder="请输入物料名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="物料类型">
                  <a-select :allowClear="true" style="width: 100%" v-model="queryParam.materialType" placeholder="请选择物料类型">
                    <a-select-option v-for="(item,index) in materialTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="密度">
                  <a-input v-model="queryParam.density" allow-clear placeholder="请输入密度"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="尺寸">
                  <a-input v-model="queryParam.specifications" allow-clear placeholder="请输入尺寸"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="物料批次">
                  <a-input v-model="queryParam.materialBatch" allow-clear placeholder="请输入物料批次"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="托盘号">
                  <a-input v-model="queryParam.containerCode" allow-clear placeholder="请输入托盘号"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="状态">
                  <a-select :allowClear="true" style="width: 100%" v-model="queryParam.status" placeholder="请选择状态">
                    <a-select-option v-for="(item,index) in statusData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>            
            </template> -->

            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px"> {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>

          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :pageSize="5"
        :pageSizeOptions="['5','10','20','30']"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('LesTakematerials:add')" >
          <a-button type="primary" v-if="hasPerm('LesTakematerials:add')" icon="plus" @click="$refs.addForm.add()">新增备料</a-button>
        </template>
        <span slot="orderStatusscopedSlots" slot-scope="text">
          <a-tag :color="text===2?'#0000ff':(text===3?'#008000':(text===4?'#ffd700':'gray'))">{{ 'order_status' | dictType(text) }}</a-tag>
        </span>
        <template slot="linkSlot" slot-scope="text,record">
          <a @click="onViewDetail(record)">{{text}}</a>
        </template>
        <span slot="action" slot-scope="text, record">
          <a :class="[(record.orderStatus===1||record.orderStatus===4)?'':'disabled']" style="margin-right:10px;" @click="onEdit(record)">编辑</a>
          <a :class="[record.orderStatus===1||record.orderStatus===4?'':'disabled']" @click="onDel(record)">删除</a>
        </span>
      </s-table>
      
      <info-block :query-id="infoId" v-if="infoId" :order-no="infoOrderNo" />
      
      <add-form ref="addForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { PrePareManagePage, PrePareManageDelete } from '@/api/modular/main/prePareManage'
  import addForm from './addForm.vue'
  import infoBlock from './infoBlock.vue'
  export default {
    components: {
      STable,
      addForm,
      infoBlock
    },
    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {},
        columns: [
          {
            title: '单据编号',
            align: 'center',
            dataIndex: 'orderNo',
            scopedSlots: { customRender: 'linkSlot' }
          },
          {
            title: '单据状态',
            align: 'center',
            dataIndex: 'orderStatus',
            scopedSlots: { customRender: 'orderStatusscopedSlots' }
          },
          {
            title: '备料总数',
            align: 'center',
            dataIndex: 'orderQuantityTotal'
          },
          {
            title: '创建时间',
            align: 'center',
            dataIndex: 'createdTime'
          },
          {
            title: '创建人',
            align: 'center',
            dataIndex: 'createdUserName'
          },
          {
            title: '操作',
            width: '120px',
            align: 'center',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return PrePareManagePage(Object.assign(parameter, this.queryParam)).then((res) => {
            let _arr = res.data.rows || []
            if (_arr.length>0){
              this.onViewDetail(_arr[0])
            }
            return res.data
          })
        },
        materialTypeData: [],
        statusData: [],
        infoId:null,
        infoOrderNo:''
      }
    },
    created () {
      const statusOption = this.$options
      this.statusData = statusOption.filters['dictData']('order_status')
      console.log(this.statusData.map((obj)=>{
        return {code:obj.code,name:obj.name}
      }))
    },
    methods: {
      onViewDetail(obj){
        if (this.infoId===obj.id) {
          this.infoId = null
          this.infoOrderNo = ''
        } else {
          this.infoId = obj.id;
          this.infoOrderNo = obj.orderNo
        }
      },
      onEdit(){
        
      },
      onDel(obj){
        if (obj.orderStatus!==1&&obj.orderStatus!==4) return false
        this.$confirm({
          title: '系统提示',
          content: '您将要进行删除的操作，确认要继续嘛？',
          okText:'确认',
          cancelText:'取消',
          onOk:()=>{
            this.dealDelete([obj.id],(f)=>{
              if (f) {
                this.$message.success('删除成功')
                if (obj.id===this.infoId) {
                  this.infoId = null
                }
                this.$refs.table.refresh()
              }
            })
          }
        });
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      dealDelete(ids,callback){
        this.$loading.show()
        PrePareManageDelete(ids).then(()=>{
          this.$loading.hide()
          this.$refs.table.refresh()
          callback && callback(true)
        }).catch(()=>{
          this.$loading.hide()
          callback && callback(false)
        })
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
